from . import db
from flask import url_for
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
class Article(db.Model):
    __tablename__ = 'admin_articles'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='文章ID')
    title = db.Column(db.String(50), comment='标题')
    content = db.Column(db.String(500), comment='内容')
    created = db.Column(db.Date, default=datetime.now().date(), comment='时间')
    author = db.Column(db.String(20), comment='作者')
    category_id = db.Column(db.Integer, comment='分类ID')
    @property
    def link(self):
        return url_for('main.article', id=self.id, _external=True)
class Category(db.Model):
    """分类"""
    __tablename__ = 'admin_category'
    id = db.Column(db.Integer, primary_key=True, comment='分类ID')
    name = db.Column(db.String(10), comment='分类名称')
class User(db.Model):
    """用户"""
    __tablename__ = 'admin_user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='用户ID')
    username = db.Column(db.String(20), comment='用户名')
    realname = db.Column(db.String(20), comment='真实名字')
    password_hash = db.Column(db.String(128), comment='哈希密码')
    create_at = db.Column(db.Date, default=datetime.now().date(), comment='创建时间')
    update_at = db.Column(db.Date, default=datetime.now().date(), onupdate=datetime.now().date(), comment='更新时间')
    def set_password(self, password):
        self.password_hash = generate_password_hash(password)
    def validate_password(self, password):
        return check_password_hash(self.password_hash, password)
class Setting(db.Model):
    """设置"""
    __tablename__ = 'admin_setting'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='设置ID')
    title = db.Column(db.String(50), comment='网站名称')
    subtitle = db.Column(db.String(100), comment='附标题')
    discription = db.Column(db.String(50), comment='描述')
    about = db.Column(db.String(100), comment='关于')
    keywords = db.Column(db.String(100), comment='关键字')
    notice = db.Column(db.String(100), comment='提示')